using System;
using UnityEngine;
using UnityEngine.UI;

public class ChatCandidateItem : MonoBehaviour
{
    MiniUserInfo userInfo;
    public long Id => userInfo.userId;

    public NetworkImage icon;

    Action<ChatCandidateItem> m_OnClick;
    Action m_OnConfirm;
    
    public Text redPointText;
    public Text contentText;
    public Text timeText;
    float iconImgSize = 100;

    //Texture2D m_Texture;
    void Awake()
    {
        iconImgSize = icon.GetComponent<RectTransform>().rect.width;
    }

    Chat m_Chat;
    public Chat chatData => m_Chat;
    internal void Init(MiniUserInfo opponentUserInfo,Chat chat, Action<ChatCandidateItem> onSelect)
    {
        m_Chat = chat;
        this.userInfo = opponentUserInfo; 
        m_OnClick = onSelect;

        if (iconImgSize<=0)
            iconImgSize = icon.GetComponent<RectTransform>().rect.width;

        icon.Init(opponentUserInfo.icon);
        //if (m_Texture)
        //    DestroyImmediate(m_Texture);
        //m_Texture = UnityFunctions.GetTextureWithByte(opponentUserInfo.icon);

        //UnityFunctions.SetRawImage(icon , m_Texture, iconImgSize,true);// GameResourceManager.LoadCandidatePhoto(chat.userId1);
      
        if (chat.dialogues.Length > 0)
        {
            var lastestDialogue = chat.dialogues[chat.dialogues.Length - 1];
            contentText.text = lastestDialogue.message;

            this.timeText.text = UnityFunctions.GetTimeElapsed(lastestDialogue.time) + "ǰ";
        }
        redPointText.transform.parent.gameObject.SetActive(chat.newMsgCount > 0);
        redPointText.text = chat.newMsgCount.ToString();
    }

    public void OnClick()
    {
        m_OnClick?.Invoke(this);
    }

    //void OnDestroy()
    //{
    //    if (m_Texture)
    //        Destroy(m_Texture);

    //}

}